import axios from '@/axios'
import { createStore } from 'vuex'
export default createStore({
	state: {
		pageVar: {
			isLoading: false,
			title: '',
			filetype: '',
			uploadsize: 2
		},
		route: JSON.parse(sessionStorage.getItem('route')) || [],
		breadcrumb: [],
		token: localStorage.getItem('token') || '',
		admin: {},
		actions: [],
		sysinfo: [],
		indexData: {},
		modInfo: JSON.parse(sessionStorage.getItem('modInfo')) || {},
		messageInfo: {
			number: 0
		},
		sock: {
			v: {
				relist: 0
			}
		}
	},
	mutations: {
		SET_PAGEVAR: (state, obj) => {
			state.pageVar.title = obj.title
			state.pageVar.filetype = obj.filetype
			state.pageVar.uploadsize = obj.uploadsize
		},
		setBreadcrumb: (state, res) => {
			state.breadcrumb = res
		},
		SET_TOKEN: (state, token) => {
			state.token = token
			localStorage.setItem('token', token)
		},
		SET_ROUTE: (state, route) => {
			state.route = route
			sessionStorage.setItem('route', JSON.stringify(route))
		},
		SET_ADMIN: (state, obj) => {
			state.admin = obj
		},
		SET_ACTIONS: (state, obj) => {
			state.actions = obj
		},
		SET_SYSINFO: (state, obj) => {
			state.sysinfo = obj
		},
		SET_INDEX_DATA: (state, data) => {
			state.indexData = data
		},
		LOGIN_OUT: (state) => {
			localStorage.removeItem('token')
			sessionStorage.removeItem('route')
			state.token = null
		},
		SET_MODINFO: (state, value) => {
			state.modInfo = value
			sessionStorage.setItem('modInfo', JSON.stringify(value))
		},
		SET_MESSAGEINFO: (state, value) => {
			state.messageInfo = value
		},
		SET_SOCK: (state, value) => {
			state.sock.v.relist++
		}
	},
	actions: {
		GET_BASE: async ({ commit }, obj) => {
			let params = {}
			if (obj) {
				params.modname = obj.params.modname
			}
			await axios.get('index', params).then((res) => {
				commit('SET_ROUTE', res.data.admin.routes)
				commit('SET_PAGEVAR', {
					title: res.data.sitetitle,
					filetype: res.data.filetype,
					uploadsize: res.data.uploadsize
				})
				commit('SET_ADMIN', res.data.admin)
				commit('SET_ACTIONS', res.data.actions)
				commit('SET_SYSINFO', res.data.sysinfo)
				commit('SET_INDEX_DATA', res.data.index_data)
				if (res.data.modinfo) {
					commit('SET_MODINFO', res.data.modinfo)
				}
				document.title = res.data.sitetitle
			})
		},
		GET_MESSAGE: async ({ commit }) => {
			const resq = await axios.post('mymessage/index')
			commit('SET_MESSAGEINFO', resq.data)
		},
		SET_SOCK: ({ commit }, value) => {
			if (value.v) {
				commit('SET_SOCK', value)
			}
		}
	},
	getters: {
		openMenuId: (state) => state.Sys.openMenuId,
		currentMenu: (state) => state.Sys.currentMenu
	}
})
